﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.UI.Adapters;

namespace ElevatorSpeeches.UI.WebForms
{
    /// <summary>
    /// Summary description for FormRewriterControlAdapter
    /// </summary>
    public class FormRewriterControlAdapter : ControlAdapter
    {
        public FormRewriterControlAdapter() { }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new RewriteFormHtmlWriter(writer));
        }
    }

    public class RewriteFormHtmlWriter : HtmlTextWriter
    {
        public RewriteFormHtmlWriter(HtmlTextWriter writer)
            : base(writer)
        {
            this.InnerWriter = writer.InnerWriter;
        }

        public RewriteFormHtmlWriter(System.IO.TextWriter writer)
            : base(writer)
        {
            this.InnerWriter = writer;
        }

        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name == "action")
            {
                value = HttpContext.Current.Request.RawUrl;

                //            if (HttpContext.Current.Items["ActionAlreadyTaken"] == null)
                //            {
                //                value = HttpContext.Current.Request.RawUrl;
                //                HttpContext.Current.Items["ActionAlreadyTaken"] = true;
                //            }
            }
            base.WriteAttribute(name, value, fEncode);

        }
    }
}


